package jee.boot.entity.system;

import jee.boot.common.annotation.Column;
import jee.boot.common.annotation.Table;
import jee.boot.common.basic.TreeEntity;
import jee.boot.common.utils.HttpServletHolder;
import jee.boot.common.utils.StringUtils;

/**
 * 字典Entity
 *
 * @author zhilei.wang
 */
@Table(name = "st_dictionary")
public class Dictionary extends TreeEntity<Dictionary> {
    private static final long serialVersionUID = 1L;
    /**
     * 父类
     */
    @Column(name = "parent_id", data = {"parent.id", "parent.name", "parent.title"})
    private Dictionary parent;
    /**
     * value值
     */
    private String value; // 数据值
    /**
     * 标签
     */
    private String label; // 标签名
    /**
     * 类型
     */
    private String type; // 类型
    /**
     * 描述
     */
    private String description;// 描述
    /**
     * 国际语言代码
     */
    private String i18nCode;
    /**
     * 国际语言
     */
    @Column(action = Column.Action.QUERY, key = "code", name = "i18n_code",data = {"i18n.id","i18n.code","i18n.label"},extraConditions = {"i18n.language=#{language}"})
    private I18n i18n;
    @Column(ignoreAlias = true, name = "(select count(1) from st_dictionary child where child.parent_id=a.id)", action = Column.Action.QUERY)
    private Integer childNum = 0;
    @Column(ignoreAlias = true, name = "(select ifnull(max(child.level)-a.level,0) from st_dictionary child where child.parent_ids like  CONCAT('%',a.id,',%'))", action = Column.Action.QUERY)
    private Integer childDeep = 0;
    public Dictionary() {
        super();
        setCacheKey(getProjectConfig().getName()+ "_dictCache_"+HttpServletHolder.getLanguage());
    }

    public Dictionary(String id) {
        super(id);
        setCacheKey(getProjectConfig().getName() + "_dictCache_"+HttpServletHolder.getLanguage());
    }
    /**
     * 父对象，只能通过子类实现，父类实现mybatis无法读取
     *
     * @return
     */
    @Override
    public Dictionary getParent() {
        return parent;
    }

    /**
     * 父对象，只能通过子类实现，父类实现mybatis无法读取
     *
     * @param parent
     * @return
     */
    @Override
    public void setParent(Dictionary parent) {
        this.parent=parent;
    }

    public Dictionary(String value, String label) {
        this.value = value;
        this.label = label;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public String getLabel() {
        return label;
    }

    public void setLabel(String label) {
        this.label = label;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getI18nCode() {
        return i18nCode;
    }

    public void setI18nCode(String i18nCode) {
        this.i18nCode = i18nCode;
    }

    public I18n getI18n() {
        return i18n;
    }
    public void setI18n(I18n i18n) {
        this.i18n = i18n;
        if(i18n!=null && StringUtils.isNotBlank(i18n.getCode())) {
            this.i18nCode = i18n.getCode();
        }
    }

}